import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import Welcome from '../components/Welcome.vue'
import User from '../components/users/User.vue'

import Rights from '../components/power/Rights.vue'
import Roles from '../components/power/Roles'

import Cate from '../components/goods/Cate.vue'
import Params from '../components/goods/Params.vue'
import Goods from '../components/goods/Goods.vue'

import Order from '../components/order/Order.vue'
import reports from '../components/reports/Reports.vue'

Vue.use(VueRouter)

const routes = [
  {
    path:'/',
    redirect:'/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: Login
  },
  {
    path: '/home',
    name: 'home',
    component:Home,
    redirect:'/welcome',
    children:[
      {path:'/welcome',component:Welcome},
      {path:'/users',component:User},
      {path:'/rights',component:Rights},
      {path:'/roles',component:Roles},
      {path:'/categories',component:Cate},
      {path:'/params',component:Params},
      {path:'/goods',component:Goods},
      {path:'/orders',component:Order},
      {path:'/reports',component:reports}
    ]
  }
]

const router = new VueRouter({
  routes
})
//路由守卫，防止没有登录的进入需要登录页
router.beforeEach((to,from,next)=>{
  if(to.path=='/login') return next();
  const tokenStr = window.sessionStorage.getItem('token')
  if(!tokenStr){
    return next('/login')
  }
  next()
})

export default router
